package com.ds.smartstudio.controller;

import com.alibaba.fastjson.JSON;
import com.ds.smartstudio.common.Constants;
import com.ds.smartstudio.common.Result;
import com.ds.smartstudio.entity.Area;
import com.ds.smartstudio.entity.Device;
import com.ds.smartstudio.entity.Type;
import com.ds.smartstudio.service.AreaService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yangsf
 * @since 2022-07-02 10:42:24
 */
@RestController
@RequestMapping("/smartstudio/area")
public class AreaController {
    @Autowired
    AreaService areaService;

    @ApiOperation(value = "area实体")
    @RequestMapping(method = RequestMethod.HEAD)
    public Area area() {
        return new Area();
    }

    @ApiOperation("该方法用于获取所有的场所")
    @GetMapping("/all")
    public String selectAll() {
        List<Area> types = areaService.list();
        return JSON.toJSONString(types);
    }

    @ApiOperation(value = "分页查询", notes = "根据页码和页面大小和name查询area，模糊匹配area的name，name在不搜索时为空字符串（默认），此方法可以在搜索功能和分页展示上使用，返回一个数组,第一个元素是当页所需要的user数组,第二个元素是当前条件的总记录数")
    @GetMapping
    public String selectPage(@ApiParam(value = "当前页码", required = true)
                             @RequestParam int page,
                             @ApiParam(value = "页面大小", required = true)
                             @RequestParam int pageSize,
                             @ApiParam("产所名称")
                             @RequestParam(defaultValue = "")  String name) {
        return areaService.areaPage(page, pageSize, name);
    }

    @ApiOperation("该方法用于添加场所，返回一个bool值")
    @PostMapping
    public Boolean addArea(@ApiParam(value = "需要一个Area实体", required = true) @RequestBody Area area) {
        return areaService.save(area);
    }

    @ApiOperation("该方法用于编辑场所，返回一个bool值")
    @PutMapping
    public Boolean updateArea(@ApiParam(value = "需要一个Area实体", required = true) @RequestBody Area area) {
        return areaService.updateById(area);
    }
    @ApiOperation("该方法用于批量删除设备，返回一个bool值")
    @DeleteMapping("/removeAll")
    public Boolean removeALL(@ApiParam("批量删除") @RequestBody List<Integer> ids){
        return areaService.removeBatchByIds(ids);
    }
    @ApiOperation("该方法用于删除场所，返回一个bool值")
    @DeleteMapping
    public Boolean deleteArea(@ApiParam(value = "需要一个area实体", required = true) @RequestBody Area area) {
        return areaService.removeById(area);
    }

    @GetMapping("/count")
    public Result countArea() {
        Result result;
        try {
            Integer cnt = areaService.selectTotalByName("");
            result = Result.succeed(cnt);
        } catch (Exception e) {
            result = Result.error(Constants.CODE_500, e.toString());
        }

        return result;
    }
}
